package com.orange.demo;

import java.util.Arrays;

/**
 * @author HongHao
 * @since 2021-08-30 22:51
 */
public class Mp4Parser implements Parser {

    public final byte[] FORMAT = "MP4".getBytes();

    public final int FORMAT_LENGTH = FORMAT.length;

    static
    {
        try {
            ParserManager.registerParser(new Mp4Parser());
            System.out.println("register   Mp4Parser");
        }
        catch (Exception e) {
            throw new RuntimeException("Can't register parser!");
        }
    }

    @Override
    public Song parse(byte[] data) throws Exception{
        if (!isDataCompatible(data)) {
            throw new Exception("data format is wrong.");
        }
        return new Song("mp4", "有没有人曾告诉你", "陈楚生", 320L);
    }

    private boolean isDataCompatible(byte[] data) {
        byte[] format = Arrays.copyOfRange(data, 0, FORMAT_LENGTH);
        return Arrays.equals(format, FORMAT);
    }


}
